<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="400px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="合同数量" prop="visaNum">
          <el-input v-model="form.visaNum" maxlength="11" placeholder="请输入合同数量" v-input.int/>
        </el-form-item>
        <el-form-item label="备注" prop="recordRemark">
          <el-input v-model="form.recordRemark" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addContractNumber, subtractContractNumber } from '@/api/electronicVisa'

export default {
  props: {
    title: {
      default: '添加合同数量'
    },
    value: {
      default: false
    },
    visaId: {
      default: undefined
    },
    tenantId: {
      default: undefined
    },
    type:{
      default: 1
    }
  },
  data() {
    return {
      visible: false,
      // 遮罩层
      loading: false,
      form: {},
      // 选中数组
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 总条数
      total: 0,
      //列表
      list: [],
      //记录类型
      recordTypeList: [
        { label: '增加', value: 0 },
        { label: '扣减', value: 1 }
      ],
      rules: {
        visaNum: [
          { required: true, message: '请输入合同数量', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if(value == 0){
                callback(new Error('添加合同数量不能为0'))
              }
              callback()
            }
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init()
      }
      this.visible = newVal // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit('input', val) // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    }
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      // this.dateRange = [];
      this.form = {
        visaId: null,
        visaNum: '', //合同数量
        tenantId: '',
        recordRemark: ''//备注
      }
      this.resetForm('form')
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.tenantId = this.tenantId
          this.form.visaId = this.visaId
          if (this.type == 1) {
            addContractNumber(this.form).then(response => {
              if (response.code == 200) {
                this.$emit('success')
                this.$message.success(response.msg)
                this.visible = false
              } else {
                this.$message.error(response.msg)
              }
            })
          } else if (this.type == 2) {
            subtractContractNumber(this.form).then(response => {
              if (response.code == 200) {
                this.$emit('success')
                this.$message.success(response.msg)
                this.visible = false
              } else {
                this.$message.error(response.msg)
              }
            })
          }

        }
      })
    },
    cancel() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
